import sqlite3
from sqlite3.dbapi2 import OperationalError
import sys

# SQLite natively supports only the types TEXT, INTEGER, REAL, 
# BLOB and NULL
def checkDb(dbPath):
    # 'file:path/to/database?mode=ro'
    con = None
    try:
        con = sqlite3.connect('file:'+dbPath+'?mode=ro', uri=True)
    except OperationalError as identifier:
        print("Db Path Invalid %s" % (dbPath))
        return
   
    cur = con.cursor()

    sqlTables = "SELECT name FROM sqlite_master where type='table'"
    # 1. 使用Row对象，逐行获取.
    print("============================")
    cur.execute(sqlTables)
    oneRow = cur.fetchone()
    array1 = []
    while(oneRow != None):
        print(oneRow[0])
        array1.append(oneRow[0])
        oneRow = cur.fetchone()

    print("============================")
    # 2. 一次过获取所有行.
    cur.execute(sqlTables)
    for one in cur.fetchall():
        print(one[0])

    print("============================")
    # 3. 统计每个表的行数.
    for one in array1:
        t = (one,)
        cur.execute("SELECT COUNT(1) FROM "+one)
        print("%s(%d) " % (one,cur.fetchone()[0]))

    # SELECT name FROM sqlite_master where type='table'
    # 获取所有表名
    # for row in con.execute("SELECT name FROM sqlite_master where type='table'"):
    #     print(row)
    
    cur.close()
    # https://docs.python.org/3/library/sqlite3.html


if __name__ == "__main__":
    checkDb(sys.argv[1])
    pass